package com.lebang.controller.api.oceanengine;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.model.back.OceanEngine;
import com.lebang.service.mongodb.OceanEngineMongodb;
import com.lebangweb.common.TextUtil;
import com.lebangweb.encrypt.MessageDigestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 巨量广告事件回调
 *
 * @Author: hwq
 * @Date: 2020/5/8 10:05
 * @Version 1.0
 */
@Slf4j
@Controller
@RequestMapping("/api/custom/v1")
public class OceanEngineApi {
    @Autowired
    private OceanEngineMongodb oceanEngineMongodb;
    @Autowired
    private MongoTemplate mongoTemplate;
    @GetMapping("/oceanengine")
    @ResponseBody
    public String oceanengine(String adid, String imei, String mac,
                              String oaid, String androidid, String os, Long TIMESTAMP,
                              String cid,
                              String callback_url, String callback, HttpServletRequest request) {
        OceanEngine vo = new OceanEngine();
        vo.setCid(cid);
        vo.setAdid(adid);
        vo.setImei(imei);
        vo.setMac(mac);
        vo.setOaid(oaid);
        vo.setAndroidid(androidid);
        vo.setOs(os);
        vo.setTIMESTAMP(TIMESTAMP);
        vo.setCreatTime(LocalDateTime.now());
        vo.setCallback_url(callback_url);
        vo.setStatus(0);
        vo.setCallback(callback);
        oceanEngineMongodb.save(vo);
//     JSONObject jsonObject=   OkHttpUtil.getRequest(callback_url);
//      log.info(jsonObject.toJSONString());
        return "ok";
    }
    @Login
    @GetMapping("/list")
    public String logs(Model model, Integer id) {
        model.addAttribute("id", id);
        return "/oceanengine/logs";
    }
    @Login
    @PostMapping("/list")
    @ResponseBody
    public JSONObject logslist(HttpServletRequest request, @RequestParam Integer page,
                               @RequestParam Integer limit, Integer version, String appStore, String imei,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date star,
                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        page = (page - 1);
        Sort sort = Sort.by(Sort.Direction.DESC, "creatTime");
        Query query = new Query();
        Criteria criteria = new Criteria();
        //设置查询时间范围
        if (!TextUtil.isEmpty(star)) {
            criteria.and("creatTime").gte(star).lte(end);
        }

        if (!TextUtil.isEmpty(imei)) {
            try {
                criteria.and("imei").is(MessageDigestUtil.encryptMD5(imei.getBytes()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        query.addCriteria(criteria);
//查询总条数
        long count = mongoTemplate.count(query, OceanEngine.class);
        Pageable pageable = PageRequest.of(page, limit,sort);
        query.with(pageable);
//查询数据
        List<OceanEngine> mongoVehicleUploadLogs = mongoTemplate.find(query.with(pageable), OceanEngine.class);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", mongoVehicleUploadLogs);
        jsonObject.put("count", count);
        return jsonObject;
    }
}
